﻿using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;

namespace DifyToOpenAI.Model
{
    public class ModelList
    {
        [JsonPropertyName("object")]
        public string? @object { get; set; } // 使用 @ 关键字是因为 "object" 是 C# 的保留关键字
        public List<ModelData>? data { get; set; } = new List<ModelData>();

        [JsonIgnore]
        private string realJsonstring = string.Empty;

        /// <summary>
        /// Gets or sets the real JSON string representation of the response.
        /// 获取或设置响应的真实 JSON 字符串表示。
        /// </summary>
        [JsonIgnore]
        public string? RealJsonstring
        {
            get { return realJsonstring; }
            set
            {
                realJsonstring = value!;
                try
                {
                    DeserializeModelList(value!);
                }
                catch (Exception)
                {
                    // 异常处理，可根据需要记录日志
                }
            }
        }

        /// <summary>
        /// 反序列化 JSON 字符串到当前的实例。
        /// 该方法会解析 JSON 中的每个属性，并将值赋给当前实例的对应属性。
        /// 如果在解析过程中出现异常，会捕获异常并继续解析后续属性。
        /// Deserialize a JSON string into the current instance.
        /// This method parses each property in the JSON and assigns the values to the corresponding properties of the current instance.
        /// If an exception occurs during the parsing process, it catches the exception and continues to parse the subsequent properties.
        /// </summary>
        /// <param name="json">需要解析的 JSON 字符串。The JSON string to be parsed.</param>
        public virtual void DeserializeModelList(string json)
        {
            var documentOptions = new JsonDocumentOptions
            {
                CommentHandling = JsonCommentHandling.Skip,
                AllowTrailingCommas = true // 允许或禁止 JSON 数据中的尾随逗号。
            };

            JsonNode? document = JsonNode.Parse(json: json, documentOptions: documentOptions)!;

            if (JsonValueKind.Array == document.GetValueKind())
            {
                JsonArray? data = null;
                try
                {
                    data = document.AsArray();
                }
                catch (Exception)
                {

                    //  throw;
                }

                if (data != null && data.Count > 0)
                {
                    this.data = new List<ModelData>();
                    foreach (JsonNode? item in data!)
                    {
                        ModelData modelData = new ModelData();
                        /*url*/
                        try
                        {
                            modelData.url = Convert.ToString(item["url"]);
                        }
                        catch (Exception)
                        {

                            //  throw;
                        }

                        /*key*/
                        try
                        {
                            modelData.key = Convert.ToString(item["key"]);
                        }
                        catch (Exception)
                        {

                            //  throw;
                        }

                        /*name*/
                        try
                        {
                            modelData.name = Convert.ToString(item["name"]);
                            modelData.owned_by = Convert.ToString(item["name"]);
                        }
                        catch (Exception)
                        {
                            modelData.name = "ERRORNAME";

                        }

                        /*modelId*/
                        try
                        {
                            modelData.id = Convert.ToString(item["modelId"]);
                        }
                        catch (Exception)
                        {
                           
                            modelData.id = "error-modelId";
                            //  throw;
                        }
                        this.data.Add(modelData);
                    }
                }

            }

            this.@object = "list";
        }



    }
    public class ModelData
    {
        public string? id { get; set; }
        [JsonPropertyName("object")]
        public string? @object { get; set; } = "model";// 使用 @ 关键字是因为 "object" 是 C# 的保留关键字
        public long created { get; set; } = 1497290254;
        public string? owned_by { get; set; } = "difytoopenai";

        [JsonIgnore]
        public string? url { get; set; }
        [JsonIgnore]
        public string? key { get; set; }
        [JsonIgnore]
        public string? name { get; set; }
    }
}
